﻿using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXDBC
    {
        public DataSet QueryDSTPLAN(DataRow dwData, int option = 1)
        {
            DataSet setReturn = new DataSet();
            DataSet Result = new DataSet();
            DataTable tbLocal = null;
            OracleCommand or1;
            // APZMANDANT  APZART APZVAR APZANRKRS APZALFDNR APZSTUFE
            switch (option)
            {
                case 1:
                    or1 = OracleDB.CreateCommand();
                    or1.CommandText = "SELECT SELECT NAME,ARTICLE,YARN,FDDIS,QTY,UNIT,REQDATE,ABPAUFKRS||'-'||ABPAUFLFD||'-'||ABPAUFPOS,"
                        + " TRIM(B.ABPART)||'.'||TRIM(B.ABPVAR)||'.'||TRIM(B.ABPFARBE) AS DYARN,"
                        + " B.ABPERLKZ,B.ABPERLDAT "
                        + "  FROM DSTPLAN P LEFT JOIN ABKOPF K "
                        + " ON TRIM(P.ORDERNO)=TRIM(K.ABKKDAUFTNR) "
                        + " LEFT JOIN ABPOS B ON K.ABKAUFKRS=B.ABPAUFKRS AND K.ABKAUFLFD=B.ABPAUFLFD "
                        + " WHERE K.ABKAUFART='GP' ";
                    break;
                case 2:
                    or1 = OracleDB.CreateCommand();
                    or1.CommandText = "SELECT NAME,ARTICLE,'J' AS JW,TRIM(Y.BAGGART)||'.'||TRIM(Y.BAGGVAR)||'.'||TRIM(Y.BAGGFARBE) AS CYARN"
                        + " FROM DSTPLAN P LEFT JOIN BASTGA Y ON"
                        + " TRIM(P.ARTICLE)=TRIM(Y.BAGART)||'-'||TRIM(Y.BAGFARBE)"
                        + " UNION"
                        + " SELECT NAME,ARTICLE,'W' AS JW,TRIM(Y.WAGGART)||'.'||TRIM(Y.WAGGVAR)||'.'||TRIM(Y.WAGGFARBE) AS CYARN"
                        + " FROM DSTPLAN P LEFT JOIN WASTGA Y ON"
                        + " TRIM(P.ARTICLE)=TRIM(Y.WAGART)||'-'||TRIM(Y.WAGWCOL)";
                    break;
                case 3:
                    or1 = OracleDB.CreateCommand();
                    or1.CommandText = "SELECT A.*,ABPAUFKRS||'-'||ABPAUFLFD||'-'||ABPAUFPOS AS ABPORDER,B.ABPLIEFDAT,B.ABPERLKZ,B.ABPERLDAT FROM "
                        + " (SELECT NAME,P.ORDERNO,ARTICLE,YARN,FDDIS,QTY,UNIT,REQDATE,'J' AS JW,TRIM(Y.BAGGART)||'.'||TRIM(Y.BAGGVAR)||'.'||TRIM(Y.BAGGFARBE) AS CYARN "
                        + " FROM DSTPLAN P LEFT JOIN BASTGA Y ON "
                        + " TRIM(P.ARTICLE)=TRIM(Y.BAGART)||'-'||TRIM(Y.BAGFARBE) "
                        + " UNION "
                        + " SELECT NAME,P.ORDERNO,ARTICLE,YARN,FDDIS,QTY,UNIT,REQDATE,'W' AS JW,TRIM(Y.WAGGART)||'.'||TRIM(Y.WAGGVAR)||'.'||TRIM(Y.WAGGFARBE) AS CYARN "
                        + " FROM DSTPLAN P LEFT JOIN WASTGA Y ON TRIM(P.ARTICLE)=TRIM(Y.WAGART)||'-'||TRIM(Y.WAGWCOL) "
                        + " ) A LEFT JOIN ABKOPF K "
                        + " ON TRIM(A.ORDERNO)=TRIM(K.ABKKDAUFTNR) "
                        + " LEFT JOIN ABPOS B ON K.ABKAUFKRS=B.ABPAUFKRS AND K.ABKAUFLFD=B.ABPAUFLFD "
                        + " AND A.CYARN=TRIM(B.ABPART)||'.'||TRIM(B.ABPVAR)||'.'||TRIM(B.ABPFARBE)"
                        + " ORDER BY ARTICLE,JW,CYARN";

                    break;
                case 4:
                    or1 = OracleDB.CreateCommand();
                    or1.CommandText = "SELECT NAME,P.ORDERNO,ARTICLE,YARN,FDDIS,QTY,UNIT,REQDATE,"
                    + " ABPAUFKRS||'-'||ABPAUFLFD||'-'||ABPAUFPOS AS ABPORDER, ABPLIEFDAT,ABPERLDAT,ABPSOLLMENGE1*0.01 AS PLANQTY"
                    + ",ABPEALFM*0.01 AS RQTY  "
                    + " FROM DSTPLAN P LEFT JOIN ABPOS B ON TRIM(P.ARTICLE)=TRIM(B.ABPART)||'-'||TRIM(B.ABPFARBE)"
                        + " WHERE B.ABPAUFART='VW'";
                    break;
                case 5:
                    or1 = OracleDB.CreateCommand();
                    or1.CommandText = "SELECT NAME,P.ORDERNO,ARTICLE,YARN,FDDIS,QTY,UNIT,REQDATE, "
                        + " ABPAUFKRS||'-'||ABPAUFLFD||'-'||ABPAUFPOS AS ABPORDER, ABPLIEFDAT,ABPSOLLMENGE2*0.01 AS PLANQTY,ABPZUGET*0.01 AS MQTY,"
                        + " ABPEBLFM*0.01 AS RQTY ,ABPERLDAT FROM DSTPLAN P  "
                        + " LEFT JOIN ABPOS B ON TRIM(P.ARTICLE)=TRIM(B.ABPART)||'-'||TRIM(B.ABPFARBE) WHERE B.ABPAUFART='PR'";
                    break;
                case 6:
                    or1 = OracleDB.CreateCommand();
                    or1.CommandText = "SELECT NAME,P.ORDERNO,ARTICLE,YARN,FDDIS,QTY,UNIT,REQDATE, "
                        + "ABPAUFKRS||'-'||ABPAUFLFD||'-'||ABPAUFPOS AS ABPORDER, ABPERLDAT,ABPSOLLMENGE1*0.01 AS PLANQTY,"
                        + "ABPSOLLMENGE2*0.01 AS PMQTY，ABPZUGET*0.01 AS MQTY"
                        + ",ABPEBLFM*0.01 AS RQTY  FROM DSTPLAN P  "
                        + "LEFT JOIN ABPOS B ON TRIM(P.ARTICLE)=TRIM(B.ABPART)||'-'||TRIM(B.ABPFARBE) WHERE B.ABPAUFART='VA'";
                    break;
                default:
                    or1 = OracleDB.CreateCommand();
                    or1.CommandText = "SELECT * FROM ABPOS WHERE  "
                    + "  ABPMANDANT=:ABPMANDANT AND ABPAUFKRS=:ABPAUFKRS AND ABPAUFLFD=:ABPAUFLFD";
                    or1.Parameters.Add("ABPMANDANT", OracleType.NVarChar).Value = dwData["ABPMANDANT"].ToString();
                    or1.Parameters.Add("ABPAUFKRS", OracleType.NVarChar).Value = dwData["ABPAUFKRS"].ToString();
                    or1.Parameters.Add("ABPAUFLFD", OracleType.NVarChar).Value = dwData["ABPAUFLFD"].ToString();
                    break;
            }

            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
            {
                tbLocal = Result.Tables[0];
                tbLocal.TableName = "DSTPLAN";
            }
            setReturn.Merge(Result);
            return setReturn;
        }
    }
}
